package com.example.demo4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.GridLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.demo4.model.CardMatchingGame

class CardGameActivity : AppCompatActivity() {
    
    private lateinit var gridCards: GridLayout
    private lateinit var tvScore: TextView
    private lateinit var btnRestart: Button
    private lateinit var btnExit: Button
    
    private lateinit var game: CardMatchingGame
    private val cardViews = mutableListOf<CardView>()
    private val CARD_COUNT = 16
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_card_game)
        
        initViews()
        initGame()
        setupListeners()
    }
    
    private fun initViews() {
        gridCards = findViewById(R.id.gridCards)
        tvScore = findViewById(R.id.tvScore)
        btnRestart = findViewById(R.id.btnRestart)
        btnExit = findViewById(R.id.btnExit)
    }
    
    private fun initGame() {
        game = CardMatchingGame(CARD_COUNT)
        cardViews.clear()
        gridCards.removeAllViews()
        
        val cardSize = (resources.displayMetrics.widthPixels - 32 * 5) / 4
        
        for (i in 0 until CARD_COUNT) {
            val cardView = CardView(this).apply {
                val params = GridLayout.LayoutParams().apply {
                    width = cardSize
                    height = cardSize
                    setMargins(8, 8, 8, 8)
                }
                layoutParams = params
                setCard(game.cardAtIndex(i))
                
                setOnClickListener {
                    onCardClicked(i)
                }
            }
            
            cardViews.add(cardView)
            gridCards.addView(cardView)
        }
        
        updateScore()
    }
    
    private fun onCardClicked(index: Int) {
        val card = game.cardAtIndex(index)
        
        // 如果卡牌已经配对成功，不响应点击
        if (card.isMatched) {
            return
        }
        
        game.chooseCardAtIndex(index)
        updateUI()
    }
    
    private fun updateUI() {
        for (i in 0 until CARD_COUNT) {
            cardViews[i].setCard(game.cardAtIndex(i))
        }
        updateScore()
    }
    
    private fun updateScore() {
        tvScore.text = game.score.toString()
    }
    
    private fun setupListeners() {
        btnRestart.setOnClickListener {
            initGame()
        }
        
        btnExit.setOnClickListener {
            val resultIntent = Intent().apply {
                putExtra("score", game.score)
            }
            setResult(RESULT_OK, resultIntent)
            finish()
        }
    }
    
    override fun onBackPressed() {
        val resultIntent = Intent().apply {
            putExtra("score", game.score)
        }
        setResult(RESULT_OK, resultIntent)
        super.onBackPressed()
    }
}

